/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.modules.storage.common.Constant;
import com.jeesite.modules.storage.entity.Bank;
import com.jeesite.modules.storage.entity.Banner;
import com.jeesite.modules.storage.service.BankService;
import com.jeesite.modules.storage.service.BannerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 银行卡Controller
 */
@Controller(value = "webbankr")
@RequestMapping(value = "${adminPath}/storage/bank")
public class BankController extends BaseController {

	@Autowired
	private BankService bankService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Bank get(String id, boolean isNewRecord) {
		return bankService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(Bank bank, Model model) {
		model.addAttribute("bank", bank);
		return "modules/storage/bank/list";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Bank> listData(Bank bank, HttpServletRequest request, HttpServletResponse response) {
		bank.setMemberId(Constant.DEFAULT_ID);
		bank.setPage(new Page<>(request, response));
		return bankService.findPage(bank);
	}

	/**
	 * 查看编辑表单
	 */
	@RequestMapping(value = "form")
	public String form(Bank bank, Model model) {
		model.addAttribute("bank", bank);
		return "modules/storage/bank/form";
	}

	/**
	 * 保存银行卡
	 */
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Bank bank) {
		try{
			bank.setMemberId(Constant.DEFAULT_ID);
			bankService.save(bank);
			return renderResult(Global.TRUE, text("保存银行卡成功！"));
		}catch (Exception ex){
			return renderResult(Global.FALSE, text(ex.getMessage()));
		}
	}
	
	/**
	 * 删除银行卡
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Bank bank) {
		try{
			bankService.delete(bank);
			return renderResult(Global.TRUE, text("删除银行卡成功！"));
		}catch (Exception ex){
			return renderResult(Global.FALSE, text(ex.getMessage()));
		}
	}
	
}